//
//  ViewController.m
//  QZH
//
//  Created by 米翊米 on 2018/5/24.
//  Copyright © 2018年 米翊米. All rights reserved.
//

#import "ViewController.h"

#import "QZHHUD.h"
#import "QZHNetWorkRequest.h"
#import "QZHReachability.h"

static NSString * const Host = @"http://116.62.168.110:8080/";

@interface ViewController ()

@property (nonatomic, strong) UIButton *postButton;
@property (nonatomic, strong) UIButton *getButton;
@property (nonatomic, strong) UIButton *uploadButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //网络请求全局默认设置
    [self defaultRequest];
    [QZHReachability reachability:^(QZHNetworkStatus status, NSString *describe) {
        [[QZHHUD HUD] textHUDWithMessage:describe afterDelay:1.0];
    }];
    
    [self.view addSubview:self.postButton];
    [self.view addSubview:self.getButton];
    [self.view addSubview:self.uploadButton];
}

#pragma mark - default
- (void)defaultRequest {
    //配置全局设置app启动时配置一次即可，每个全局参数需在合适的地方配置
    //备注：下列参数也可以每个接口单独配置
    //请求方式可以不配置有默认值
    [QZHNetworkConfig globalCofig].requestType = QZHRequestTypePOST;
    //请求数据格式可以不配置有默认值
    [QZHNetworkConfig globalCofig].paramsType = QZHParamsTypeForm;
    //请求响应数据格式可以不配置有默认值
    [QZHNetworkConfig globalCofig].responeType = QZHResponeTypeJSON;
    //服务端地址全局
    [QZHNetworkConfig globalCofig].severPath = Host;
    //全局header
    [QZHNetworkConfig globalCofig].headerFields[@"token"] = @"xxxxx";
    //全局实体参数
    [QZHNetworkConfig globalCofig].params[@"test"] = @"xxxxx";
    
    //全局请求时操作，如添加提示框
    [QZHNetworkConfig globalCofig].requestAction = ^(QZHNetworkConfig *config) {
        [[QZHHUD HUD] loadingHUD];
    };
    
    //全局响应操作，如token失效登出操作
    [QZHNetworkConfig globalCofig].resultAction = ^(QZHRespModel *respModel) {
        NSLog(@"action = %@", respModel.data);
    };
}

#pragma mark - method
//普通请求示例、post
- (void)actionRequest {
    //请求示例POST
    [[QZHNetWorkRequest shareResuest] fetchConfigBlock:^(QZHNetworkConfig *config) {
        config.severPath = @"app/login";
        config.params[@"phone"] = @"17816869002";
        config.params[@"password"] = @"123456";
    } result:^(QZHRespModel *respModel) {
        if (respModel.code == QZHCodeSucces) {
            [[QZHHUD HUD] textHUDWithMessage:@"请求成功" afterDelay:1.0];
        } else {
            [[QZHHUD HUD] textHUDWithMessage:respModel.msg afterDelay:1.0];
        }
    }];
}

//普通请求示例、get
- (void)actionGet {
    //请求示例GET
    [[QZHNetWorkRequest shareResuest] fetchConfigBlock:^(QZHNetworkConfig *config) {
        //局部配置
        [config removeGlobalHeader:@"token"];
        [config removeGlobalParam:@"test"];
        config.severHost = @"https://github.com";
        config.requestType = QZHRequestTypeGET;
        config.responeType = QZHResponeTypeDefault;
    } result:^(QZHRespModel *respModel) {
        [[QZHHUD HUD] hiddenHUD];
        NSLog(@"%@", respModel.data);
    }];
}

- (void)actionUpload {
    //图片
    NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"test"], 1);
    //音频
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"黄昏里" ofType:@"mp3"];
    NSData *audioData= [NSData dataWithContentsOfFile:filePath];
    
    //文件传输示例
    [[QZHNetWorkRequest shareResuest] uploadFileConfigBlock:^(QZHNetworkConfig *config) {
        config.severPath = @"pic/upload";
        config.params[@"type"] = @"APP";
    
        //示例:传输不同文件
        //1.图片
        QZHFileModel *model1 = [[QZHFileModel alloc] init];
        model1.fileType = QZHFileImage;
        model1.fileKey = @"file";
        model1.fileName = @"tes.png";
        model1.fileData = imageData;
        //此参数在fileName为空时必须设置，内部会按照时间戳和随机数生成文件名
        //model1.fileExp = QZHFileExpPNG;
        
        //2.音频
        QZHFileModel *model2 = [[QZHFileModel alloc] init];
        model2.fileType = QZHFileAudio;
        model2.fileKey = @"file";
        model2.fileData = audioData;
        //此参数在fileName为空时必须设置，内部会按照时间戳和随机数生成文件名
        model2.fileExp = QZHFileExpMP3;
        
        config.fileDatas = @[model1, model2];
    } result:^(QZHRespModel *respModel) {
        if (respModel.code == QZHCodeSucces) {
            [[QZHHUD HUD] textHUDWithMessage:@"文件上传成功" afterDelay:1.0];
        } else {
            [[QZHHUD HUD] textHUDWithMessage:respModel.msg afterDelay:1.0];
        }
    }];
    
//    //取消单个请求，根据请求的方法
//    [QZHNetWorkRequest cancleRequest:@"pic/upload"];
//    //取消所有请求
//    [QZHNetWorkRequest cancleAllRequest];
}

#pragma mark - getter
- (UIButton *)postButton {
    if (!_postButton) {
        _postButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _postButton.frame = CGRectMake(100, 150, 200, 50);
        _postButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_postButton setTitle:@"POST请求" forState:UIControlStateNormal];
        [_postButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_postButton addTarget:self action:@selector(actionRequest) forControlEvents:UIControlEventTouchUpInside];
        _postButton.backgroundColor = [UIColor blueColor];
    }
    
    return _postButton;
}

- (UIButton *)getButton {
    if (!_getButton) {
        _getButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _getButton.frame = CGRectMake(100, 220, 200, 50);
        _getButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_getButton setTitle:@"GET请求" forState:UIControlStateNormal];
        [_getButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_getButton addTarget:self action:@selector(actionGet) forControlEvents:UIControlEventTouchUpInside];
        _getButton.backgroundColor = [UIColor blueColor];
    }
    
    return _getButton;
}

- (UIButton *)uploadButton {
    if (!_uploadButton) {
        _uploadButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _uploadButton.frame = CGRectMake(100, 290, 200, 50);
        _uploadButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_uploadButton setTitle:@"上传文件" forState:UIControlStateNormal];
        [_uploadButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_uploadButton addTarget:self action:@selector(actionUpload) forControlEvents:UIControlEventTouchUpInside];
        _uploadButton.backgroundColor = [UIColor blueColor];
    }
    
    return _uploadButton;
}

#pragma mark - system
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
